package  
{
	import content.ContentManager;
	import drawing.SpriteBatch;
	import flash.display.BitmapData;
	import flash.display.Stage;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import utils.Direction;
	/**
	 * ...
	 * @author sailor
	 */
	public class Luna 
	{
		//正在使用的地图
		private var _map:Map;
		private var _spriteBitch:SpriteBatch;
		
		//镜头
		private var _camera:Camera;
		private var _viewPort:Rectangle;
		
		private var _currentCellPosition:Point = new Point( -1, -1);
		
		private var mapTexturesManager:ContentManager;
		private var cells:Array;
		
		
		/**
		 * 创建 Luna的新实例
		 * @param	stage	舞台
		 * @param	viewPort	视口
		 */
		public function Luna(stage:Stage, viewPort:Rectangle) 
		{
			stage.color = 0xFF0000;
			
			_spriteBitch = new SpriteBatch(stage, viewPort);
			_camera = new Camera();
			_camera.viewPort = viewPort;
			_viewPort = viewPort;
			InputSystem.register(stage);
		}
		  
		
		/**
		 * 设置地图
		 * @param	map
		 */
		public function setMap(map:Map):void 
		{
			_map = map;
			_camera.map = map;

			mapTexturesManager = new ContentManager(_map.mapTexturePath, _map.mapTextureType);
			updateContent();
		}
		
		/**
		 * 地图坐标(像素为单位) 转换为Tile为单位
		 * @param	mapPosition
		 * @return
		 */
		public function mapPosToTilePos(mapPosition:Point):Point 
		{
			var pos:Point = new Point();
			pos.x = int(mapPosition.x / _map.tileWidth);
			pos.y = int(mapPosition.y / _map.tileHeight);
			return pos;
		}
		
		/**
		 * 判断角色是否可以向某个方向移动
		 * @param	playerPosition
		 * @param	dir
		 * @return
		 */
		public function canPlayerMove(playerPosition:Point, dir:int):Boolean
		{
			var targetPosition:Point = playerPosition.add(Direction.getIncrement(dir));
		
			var tile:Tile = _map.getTile(targetPosition.x, targetPosition.y);
			if (tile == null)
				return false;
			else return tile.walkable;
		}
		
		/**
		 * 更新地图文件 
		 */
		private function updateContent():void 
		{
			if (_currentCellPosition.x != _camera.cellPosition.x ||
				_currentCellPosition.y != _camera.cellPosition.y)
			{
				_currentCellPosition.x = _camera.cellPosition.x;
				_currentCellPosition.y = _camera.cellPosition.y;
				
				var maxY:int = 3, maxX:int = 3;
				if (_currentCellPosition.x + maxX > _camera.cellsAcross)
					maxX = _camera.cellsAcross - _currentCellPosition.x;
				if (_currentCellPosition.y + maxY > _camera.cellsDown)
					maxY = _camera.cellsDown - _currentCellPosition.y;
					 
				cells = [];
				for (var y:int = 0; y < maxY; y++ )
				{
					for (var x:int = 0; x < maxX; x++ )
					{
						var index:int = _currentCellPosition.x + x +(_currentCellPosition.y +  y) * _camera.cellsAcross;
						
						cells.push(index);
					}
				}
				mapTexturesManager.update(cells);
			}
		}
		
		/**
		 * 更新游戏逻辑
		 */
		public function update():void 
		{
			if (!_camera.isMoving)
			{
				if (InputSystem.mouseStats.rightDown)
				{
					var pos:Point = InputSystem.getMousePosition();
					pos.x -= _viewPort.x;
					pos.y -= _viewPort.y;
					pos.x += _camera.location.x;
					pos.y += _camera.location.y;
					
					var tilePos:Point = mapPosToTilePos(pos);
					var partyLeaderTilePosition:Point = mapPosToTilePos(_camera.partyLeaderPosition);
					var dir:int = Direction.getDiction(partyLeaderTilePosition, tilePos);
					if (dir != Direction.NONE && canPlayerMove(partyLeaderTilePosition, dir))
						_camera.creatMovement(dir);
				}
			}
			else 
			{
				_camera.update();
				updateContent();
			}
		}
		
		/**
		 * 绘制游戏界面
		 */
		public function draw():void 
		{
			_spriteBitch.clear(0);
			_spriteBitch.begin();
			
			var offset:Point = new Point(_camera.location.x % _camera.cellWidth, _camera.location.y % _camera.cellHeight);

			for (var i:int = 0, len:int = cells.length; i < len; i++ )
			{
				var x:int = cells[i]  % _camera.cellsAcross;
				var y:int = int(cells[i] / _camera.cellsAcross);
				
				x -= _currentCellPosition.x;
				y -= _currentCellPosition.y;
				
				var texture:* = mapTexturesManager.getContent(cells[i]);
				if (texture != null)
				{
					_spriteBitch.copyPixels(texture.bitmapData, new Point( -offset.x + x * _camera.cellWidth , -offset.y + y * _camera.cellHeight));
				}
			}
			
			var bmpd:BitmapData = new BitmapData(20, 20, false, 0xFFFF000000);
			
			_spriteBitch.copyPixels(bmpd, _camera.partyLeaderScreenPosition);
			
			_spriteBitch.end();
		}	
	}

}